package controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.mysql.StaffNotFoundException;

import BizLogic.LoginCredentialsManager;

/**
 * Servlet implementation class ForgetPassword
 */
@WebServlet("/ForgetPassword")
public class ForgetPasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ForgetPasswordServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String StaffID=request.getParameter("StaffID");
		LoginCredentialsManager vu=new LoginCredentialsManager();
		String SecurityQuestion=null;
		if(StaffID.trim().equals("")){
			request.setAttribute("result", "StaffID is empty!");
			RequestDispatcher rdDispatchers=request.getRequestDispatcher("ForgetPassword.jsp");
			rdDispatchers.forward(request, response);
		
		}
		else{
		try {
			try {
				SecurityQuestion=vu.forgetPassword(StaffID);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("hello");
			HttpSession StaffSession=request.getSession();
			StaffSession.setAttribute("StaffID", StaffID);
			RequestDispatcher rdDispatcher=request.getRequestDispatcher("Security.jsp");
			request.setAttribute("Question", SecurityQuestion);
			rdDispatcher.forward(request, response);
		} catch (StaffNotFoundException e) {
			// TODO Auto-generated catch block
			
			request.setAttribute("result", "Invalide StaffID!");
			RequestDispatcher rdDispatchers=request.getRequestDispatcher("ForgetPassword.jsp");
			rdDispatchers.forward(request, response);
		
		}
		}
	}

}
